package cn.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import cn.dao.DanganDao;
import cn.entity.TBanji;
import cn.entity.TDangan;
import cn.entity.TStudent;
import cn.entity.TTeacher;
import cn.util.PageBean;
@Controller("danganAction")
@Scope("prototype")
public class DanganAction extends BaseAction{
	private Integer id;
	private Integer uid;
	private String name;
	private TDangan dangan=new TDangan();
	private List<TDangan> list;
	@Autowired
	@Qualifier("danganDao")
	private DanganDao danganDao;

	
	private PageBean pageBean=new PageBean();
	
	public String list(){
		String sqlCount="select count(*) from TDangan u where 1=1";
		String sql="from TDangan u where 1=1";
		if(name!=null && !"".equals(name)){
			sqlCount+=" and u.student.name like '%"+name+"%'";
			sql+=" and u.student.name like '%"+name+"%'";
		}
		//毕业生查看自己的
		if((Integer)session.getAttribute("role")==1){
			TStudent student=(TStudent)session.getAttribute("currentUser");
			sqlCount+=" and u.student.id="+student.getId();
			sql+=" and u.student.id="+student.getId();
		}
		//辅导员查看自己学生的
		if((Integer)session.getAttribute("role")==3){
			TTeacher teacher=(TTeacher)session.getAttribute("currentUser");
			sqlCount+=" and u.student.teacher.id="+teacher.getId();
			sql+=" and u.student.teacher.id="+teacher.getId();
		}
		sqlCount+=" and u.isdel=0";
		sql+=" and u.isdel=0";
		pageBean.setTotalCount(danganDao.getCount(sqlCount));
		list=danganDao.getByPage(sql, pageBean.getPageNo(), pageBean.getPageSize());
		return SUCCESS;
	}
	
	public String listJson() throws IOException{
		String sql="from TDangan u where u.isdel=0";
		list=danganDao.getByHql(sql);
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.print(JSONArray.toJSONString(list));
		out.close();
		return null;
	}
	
	public String toupdate(){
		String sql="from TDangan u where u.isdel='"+uid+"'";
		list=danganDao.getByHql(sql);
		id=list.get(0).getId();
		
		
		dangan=danganDao.get(TDangan.class, id);
		return "detail";
	}
	
	public String add(){
		dangan.setIsdel(0);
		danganDao.add(dangan);
		return "list";
	}
	
	public String detail() throws IOException{
		response.setContentType("application/json;charset=UTF-8");
		PrintWriter out=response.getWriter();
	
		dangan=danganDao.get(TDangan.class, id);		
		out.print(JSON.toJSONString(dangan));
		out.close();
		return null;
	}
	
	public String update(){
		Integer role=(Integer)session.getAttribute("role");
		TDangan da=danganDao.get(TDangan.class, id);
		if(dangan.getDanganno()!=null && !"".equals(dangan.getDanganno())){
			da.setDanganno(dangan.getDanganno());
		}
		if(dangan.getDocpath()!=null && !"".equals(dangan.getDocpath())){
			da.setDocpath(dangan.getDocpath());
		}
		danganDao.update(da);
		if(role==1){
			try {
				response.sendRedirect("dangan_toupdate?id="+da.getId());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		}else{
			return "list";
		}
	}
	
	
	public String delete(){
		TDangan s=danganDao.get(TDangan.class, id);
		s.setIsdel(1);
		danganDao.update(s);
		return "list";
	}
	


	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}


	public TDangan getDangan() {
		return dangan;
	}

	public void setDangan(TDangan dangan) {
		this.dangan = dangan;
	}

	public List<TDangan> getList() {
		return list;
	}

	public void setList(List<TDangan> list) {
		this.list = list;
	}

	public DanganDao getDanganDao() {
		return danganDao;
	}

	public void setDanganDao(DanganDao danganDao) {
		this.danganDao = danganDao;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}


	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	

}
